<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2016/11/18
 * Time: 11:30
 */
class CrowdFundingAction extends Action{
    //众筹开奖
    public function crowdLottery($pid){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user["customerId"];
        Session::switchCustomer(0);
        $productModel = M("module_product");
        $lotteryModel = M("module_mill_lottery");
        $crowdNumber = M("module_crowdfunding_number");
        $crowdLottery = M("module_crowdfunding_lottery");
        $spec = M("module_product_specification");
        Session::switchCustomer($cid);
        $crowdStatus = $productModel->where("id=$pid")->getField("crowdStatus");
        if($crowdStatus>1){
            Output::instance()->resultCode();
            return;
        }
        $stallList =$spec->alias("a")
            ->join("inner join wx_module_product_crowdfunding b on a.firstSpecificId=b.specId and a.pid = b.pid")
            ->where("a.pid = $pid and b.drawStall=1")
            ->field("b.*")
            ->group(" b.id")
            ->select();
        foreach($stallList as $key=>$stallInfo){
            $stallId = $stallInfo['id'];
            $num = ceil($stallInfo['supportPerson']/$stallInfo['drawPerson']);
            $i = 0;
            $start = 0;
            do{
                $lotteryList = $lotteryModel->limit($start,1)->order("lotteryTime desc")->select();
                $lotteryInfo = $lotteryList[0];
                $str = str_replace(",","",$lotteryInfo['lotteryNumber']);
                $number = $str%$stallInfo['drawPerson']+100001;
                $numberInfo = $crowdNumber->where("pid = $pid and number = '$number' and status=0")->find();
                if($numberInfo){
                    $data=null;
                    $data['cid']=$cid;
                    $data['stallId']=$stallId;
                    $data['lotteryId']=$lotteryInfo['id'];
                    $data['wNumber']=$number;
                    $data['createTime']=date("Y-m-d H:i:s");
                    $crowdLottery->add($data);
                    $crowdNumber->where("id=".$numberInfo['id'])->setField("status",1);
                    $i++;
                }
                $start++;
            }while($i<$num);
        }
        $productModel->where("id=$pid")->setField("crowdStatus",3);
        Output::instance()->resultCode();
    }
    //众筹商品列表
    public function crowdProductList($name='',$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user["customerId"];
        Session::switchCustomer(0);
        $productModel = M("module_product");
        Session::switchCustomer($cid);
        $where['deleted']=array("eq",0);
        $where['playType']=array("eq",6);
        $where['fromCustomerId'] = array("eq",$cid);
        $name?$where['name']=array("like","%$name%"):"";
        $productList = $productModel->where($where)->limit($start,$limit)->order("createTime desc")->select();
        foreach($productList as $index=>$product){
            $json = json_decode($product['json'],true);
            $productList[$index]['crowTarget'] = $json['crowTarget'];
            $productList[$index]['crowStarttime'] = $json['crowStarttime'];
            $productList[$index]['crowEndtime'] = $json['crowEndtime'];
            $json['supportSum']?$productList[$index]['supportSum'] = $json['supportSum']:$productList[$index]['supportSum']=0;
            $json['supportPerson']?$productList[$index]['supportPerson'] = $json['supportPerson']:$productList[$index]['supportPerson'] =0;
        }
        if($productList){
            $count = $productModel->where($where)->count();
            Output::instance()->objArray($productList,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //众筹退款
    public function crowdFundingRefund($pid){
        Session::switchCustomer(0);
        $orderModel = M("module_product_order");
        $productModel = M("module_product");
        $crowdStatus = $productModel->where("id=$pid")->getField("crowdStatus");
        if($crowdStatus==2){
            Output::instance()->resultCode();
            return;
        }
        $where['b.pid']=array("eq",$pid);
        $where['a.status']=array("eq","supported");
        $orderList = $orderModel->alias("a")
            ->join("inner join wx_module_product_order_detail b on a.id=b.oid")
            ->where($where)
            ->field("a.id,a.price")
            ->select();
        foreach($orderList as $key=>$value){
            R("Product/orderRefund",array($value['id'],$value['price']));
        }
        $result = $productModel->where("id=$pid")->setField("crowdStatus",2);
        Output::instance()->resultCode();
    }
    //众筹订单
    public function crowdOrderList($pid,$serialNumber=null,$expressNo=null,$startTime=null,$endTime=null,
                                   $phone=null,$status=null,$fullName=null,$start=0,$limit=10,$isExport=null){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user["customerId"];
        Session::switchCustomer(0);
        $orderModel = M("module_product_order");
        Session::switchCustomer($cid);
        $where['b.pid']=array("eq",$pid);
        $where['a.fromCustomerId']=array("eq",$cid);
        $where['a.wxTransactionId']=array("neq",'');
		$where['a.orderType']=array("eq",'6');
        $serialNumber?$where['a.serialNumber']=array("like","%$serialNumber%"):"";
        $expressNo?$where['a.expressNo']=array("like","%$expressNo%"):"";
        $fullName?$where['a.fullName']=array("like","%$fullName%"):"";
        $phone?$where['a.phone']=array("like","%$phone%"):"";
        $status?$where['a.status']=array("eq","$status"):"";
        $startTime?$where['a.createTime']=array("gt","$startTime"):"";
        $endTime?$where['a.createTime']=array("lt","$endTime"):"";
        if(!$isExport){
            $arr = $orderModel->alias("a")
                ->join("inner join wx_module_product_order_detail b on a.id=b.oid")
                ->join("left join wx_module_product_order_detail_specification c on b.id=c.did")
                ->field("a.id,a.serialNumber,a.expressNo,a.fullName,b.name,c.firstSpecificName,c.secSpecificName,b.count,b.price,a.payTime,a.status")
                ->where($where)
                ->limit($start,$limit)
                ->order("a.createTime desc")
                ->select();
            if($arr){
                $count = $orderModel->alias("a")
                    ->join("inner join wx_module_product_order_detail b on a.id=b.oid")
                    ->where($where)->count();
                Output::instance()->objArray($arr,$count);
            }else{
                Output::instance()->resultCode();
            }
        }else{
            $arr = $orderModel->alias("a")
                ->join("inner join wx_module_product_order_detail b on a.id=b.oid")
                ->join("left join wx_module_product_order_detail_specification c on b.id=c.did")
                ->field("a.id,a.serialNumber,a.expressNo,a.address,a.fullName,a.phone,b.name,c.firstSpecificName,c.secSpecificName,b.count,b.price,a.payTime,a.status")
                ->where($where)
                ->order("a.createTime desc")
                ->limit($start,$limit)
                ->select();
            $data[0] = array(
                'serialNumber'=>'订单号',
                'name'=>'商品名称',
                'firstSpecificName'=>'规格1',
                'secSpecificName'=>'规格2',
                'count' => '数量',
                'fullName'=>'买家姓名',
                'phone' => '买家手机电话',
                'expressNo'=>'物流单号',
                'address' => '买家收货地址',
                'payTime'=>'下单时间',
                'isNum'=>'4',
                'istitle'=>'1'
            );
            foreach ($arr as $k1 => $v1) {
                $data[$k1+1]['serialNumber']=$v1['serialNumber'];
                $data[$k1+1]['name']=$v1['name'];
                $data[$k1+1]['firstSpecificName']=$v1['firstSpecificName'];
                $data[$k1+1]['secSpecificName']=$v1['secSpecificName'];
                $data[$k1+1]['count']=$v1['count'];
                $data[$k1+1]['fullName'] = $v1['fullName'];
                $data[$k1+1]['phone'] = $v1['phone'];
                $data[$k1+1]['expressNo']=$v1['expressNo'];
                $data[$k1+1]['address'] = $v1['address'];
                $data[$k1+1]['payTime']=$v1['payTime'];
            }
            Output::instance()->objArrayExcel('订单信息'.date("Y-m-d H:i:s"), '', $data);
        }
    }
    //中奖名单
    public function crowdWinner($pid,$serialNumber=null,$nickname=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user["customerId"];
        Session::switchCustomer(0);
        $crowdNumber = M("module_crowdfunding_number");
        Session::switchCustomer($cid);
        $where['a.status']=array("eq",'1');
        $where['a.cid']=array("eq",$cid);
        $where['a.pid']=array("eq",$pid);
        $serialNumber? $where['b.serialNumber']=array("like","%$serialNumber%"):"";
        $nickname? $where['e.nickname']=array("like","%$nickname%"):"";
        $arr = $crowdNumber->alias("a")
            ->join("inner join wx_module_product_order b on a.oid = b.id")
            ->join("inner join wx_module_product_order_detail c on b.id = c.oid")
            ->join("inner join wx_module_product_order_detail_specification d on c.id=d.did")
            ->join("wx_".$cid."_user_weixin_info e on a.userName=e.openid")
            ->field("a.id,a.number,a.oid,b.serialNumber,d.firstSpecificName,d.secSpecificName,e.nickname,e.headimgurl")
            ->where($where)
            ->limit($start,$limit)
            ->select();
        if($arr){
            $count = $crowdNumber->alias("a")
                ->join("inner join wx_module_product_order b on a.oid = b.id")
                ->join("inner join wx_module_product_order_detail c on b.id = c.oid")
                ->join("inner join wx_module_product_order_detail_specification d on c.id=d.did")
                ->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }

    }
}